package com.lin.views;


import java.io.File;


import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;

/**
 * sd卡管理类
 * 
 * @author kapai8
 * 
 */
public class SdCardManger {
	// 数据存储位置，在SD卡还是内置存储卡
	private static String mStorePath;

	/**
	 * 获取存储路径
	 * 
	 * @param mContext
	 * @return
	 */
	public static String getStorePath(Context mContext) {
		// 如果SD卡存在，返回SD卡路径
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {
			mStorePath = Environment.getExternalStorageDirectory().toString();
		} else {// 如果SD卡不存在，返回App缓存路径
			mStorePath = mContext.getCacheDir().toString();
		}
//		 Log.i("tag", "-------------path-------------" + mStorePath);
		return mStorePath;
	}

	/**
	 * 获取当前存储路径的可用空间
	 * 
	 * @param mContext
	 * @return
	 */
	public static long getAvaliableSize(Context mContext) {
		// 如果当前存储路径为空，重新获取存储路径
		if (TextUtils.isEmpty(mStorePath)) {
			getStorePath(mContext);
		}
		// 如果再次获取仍旧获取不到存储路径，设置可用空间为-1，表示无可用空间
		if (mStorePath == null) {
			return -1;
		}
		StatFs mStatFs = new StatFs(mStorePath);
		// //获取存储碎片的个数
		long blockSize = mStatFs.getBlockSize();
		// 获取
		long avaliableSize = mStatFs.getAvailableBlocks();
		return avaliableSize * blockSize;
	}

	/**
	 * 获得存储离线牌库的路径
	 * 
	 * @param mContext
	 */
	public static String getSaveDbPath(Context mContext) {
		String path = null;
		if (mStorePath == null) {
			getStorePath(mContext);
		}
		path = mStorePath + File.separator + "kapai" + File.separator + "data";
		File mFile = new File(path);
		if (!mFile.exists()) {
			mFile.mkdirs();
		}
		return path;
	}

	public static String getSaveCachePath(Context mContext) {
		String path = null;
		if (mStorePath == null) {
			getStorePath(mContext);
		}
		path = mStorePath + File.separator + "kapai" + File.separator + "cache";
		File mFile = new File(path);
		if (!mFile.exists()) {
			mFile.mkdirs();
		}
		return path;
	}
}
